package service

import (
	"two_exam/config"
	"two_exam/internal/model/dao"
)

func List(page, pageSize int, name string) ([]dao.UserChat, error) {
	var user []dao.UserChat
	query := config.DB.Offset((page - 1) * pageSize).Limit(pageSize).Find(&user)
	if name != "" {
		query = query.Where("user_name like ?", "%"+name+"%")
	}
	query.Find(&user)
	return user, nil
}

func FindUserIsExists(userName string) bool {
	var user dao.UserChat
	err := config.DB.Where("user_name = ?", userName).First(&user).Error
	if err != nil {
		return false
	}
	return true
}
func IsExistsFriend(userId, friendId int) bool {
	var user dao.UserChat
	//是否已经添加
	err := config.DB.Where("user_id = ? and friend_id = ?", userId, friendId).First(&user).Error
	if err != nil {
		return false
	}
	return true
}

func AddFriend(userId, friendId int) error {
	user := dao.UserChat{
		UserId:   userId,
		FriendId: friendId,
	}
	return config.DB.Create(&user).Error

}
